home *** CD-ROM | disk | FTP | other *** search
/ The Very Best of Atari Inside / The Very Best of Atari Inside 1.iso / mint / mint110s / signal.c < prev    next >
C/C++ Source or Header  |  1994-02-09  |  17KB  |  626 lines

  1. /*
  2. Copyright 1990,1991,1992 Eric R. Smith.
  3. Copyright 1992,1993,1994 Atari Corporation.
  4. All rights reserved.
  5. */
  6.  
  7. /* signal.c:: signal handling routines */
  8.  
  9. #include "mint.h"
  10.  
  11. void (*sig_routine)();    /* used in intr.s */
  12. short sig_exc;        /* used in intr.s */
  13.  
  14. /*
  15.  * killgroup(pgrp, sig): send a signal to all members of a process group
  16.  * returns 0 on success, or an error code on failure
  17.  */
  18.  
  19. long
  20. killgroup(pgrp, sig)
  21.     int pgrp, sig;
  22. {
  23.     PROC *p;
  24.     int found = 0;
  25.  
  26.     TRACE(("killgroup %d %d", pgrp, sig));
  27.  
  28.     if (pgrp < 0)
  29.         return EINTRN;
  30.  
  31.     for (p = proclist; p; p = p->gl_next) {
  32.         if (p->pgrp == pgrp) {
  33.             post_sig(p, sig);
  34.             found++;
  35.         }
  36.     }
  37.     if (found) {
  38.         check_sigs();    /* see if the current process is affected */
  39.         return 0;
  40.     }
  41.     else {
  42.         DEBUG(("killgroup: no processes found"));
  43.         return EFILNF;
  44.     }
  45. }
  46.  
  47. /* post_sig: post a signal as being pending. It is assumed that the
  48.    caller has already verified that "sig" is a valid signal, and
  49.    moreover it is the caller's responsibility to call check_sigs()
  50.    if it's possible that p == curproc
  51.  */
  52.  
  53. void
  54. post_sig(p, sig)
  55.     PROC *p;
  56.     int sig;
  57. {
  58.     ulong sigm;
  59.  
  60. /* if process is ignoring this signal, do nothing
  61.  * also: signal 0 is SIGNULL, and should never be delivered through
  62.  * the normal channels (indeed, it's filtered out in dossig.c,
  63.  * but the extra sanity check here is harmless). The kernel uses
  64.  * signal 0 internally for some purposes, but it is handled
  65.  * specially (see supexec() in xbios.c, for example).
  66.  */
  67.     if (p->sighandle[sig] == SIG_IGN || sig == 0)
  68.         return;
  69.  
  70. /* if the process is already dead, do nothing */
  71.     if (p->wait_q == ZOMBIE_Q || p->wait_q == TSR_Q)
  72.         return;
  73.  
  74. /* mark the signal as pending */
  75.     sigm = (1L << (unsigned long)sig);
  76.     p->sigpending |= sigm;
  77.  
  78. /* if the signal is masked, do nothing further */
  79. /* note: some signals can't be masked, and we handle those elsewhere so
  80.  * that p->sigmask is always valid. SIGCONT is among the unmaskable
  81.  * signals
  82.  */
  83.     if ( (p->sigmask & sigm) != 0 )
  84.         return;
  85.  
  86. /* otherwise, make sure the process is awake */
  87.     if (p->wait_q && p->wait_q != READY_Q) {
  88.         short sr = spl7();
  89.         rm_q(p->wait_q, p);
  90.         add_q(READY_Q, p);
  91.         spl(sr);
  92.     }
  93. }
  94.  
  95. /*
  96.  * check_sigs: see if we have any signals pending. if so,
  97.  * handle them.
  98.  */
  99.  
  100. void
  101. check_sigs()
  102. {
  103.     ulong sigs, sigm;
  104.     int i;
  105.     short deliversig;
  106.  
  107.     if (curproc->pid == 0) return;
  108. top:
  109.     sigs = curproc->sigpending & ~(curproc->sigmask);
  110.     if (sigs) {
  111.         sigm = 2;
  112. /* with tracing we need a mechanism to allow a signal to be delivered
  113.  * to the child (curproc); Fcntl(...TRACEGO...) passes a SIGNULL to indicate that we
  114.  * should really deliver the signal, hence its always safe to remove it
  115.  * from pending.
  116.  */
  117.         deliversig = (curproc->sigpending & 1L);
  118.         curproc->sigpending &= ~1L;
  119.  
  120.         for (i = 1; i < NSIG; i++) {
  121.             if (sigs & sigm) {
  122.                 curproc->sigpending &= ~sigm;
  123.                 if (curproc->ptracer && !deliversig &&
  124.                     i != SIGCONT) {
  125.                     TRACE(("tracer being notified of signal %d", i));
  126.                     stop(i);
  127.         /* the parent may reset our pending signals, so check again */
  128.                     goto top;
  129.                 } else {
  130.                     ulong omask;
  131.  
  132.                     curproc->sigpending &= ~sigm;
  133.                     omask = curproc->sigmask;
  134.  
  135.         /* sigextra gives which extra signals should also be masked */
  136.                     curproc->sigmask |= curproc->sigextra[i] | sigm;
  137.                     handle_sig(i);
  138.  
  139.  
  140. /*
  141.  * POSIX.1-3.3.4.2(723) "If and when the user's signal handler returns
  142.  * normally, the original signal mask is restored."
  143.  *
  144.  * BUG?: This unmasking could unmask a pending signal which we will not
  145.  * see this time around (if the signal number is less than i) and which
  146.  * was not pending when we started; should we detect this condition and
  147.  * loop around for a second try? POSIX only guarantees delivery of
  148.  * one signal per kernel entry, so this shouldn't really be a problem.
  149.  */
  150.                     curproc->sigmask = omask;    /* unmask signals */
  151.                 }
  152.             }
  153.             sigm = sigm << 1;
  154.         }
  155.     }
  156. }
  157.  
  158. /*
  159.  * raise: cause a signal to be raised in the current process
  160.  */
  161.  
  162. void
  163. raise(sig)
  164.     int sig;
  165. {
  166.     post_sig(curproc, sig);
  167.     check_sigs();
  168. }
  169.  
  170. #ifdef EXCEPTION_SIGS
  171. /* exception numbers corresponding to signals */
  172. char excep_num[NSIG] =
  173. { 0, 0, 0, 0,
  174.   4,            /* SIGILL == illegal instruction */
  175.   9,            /* SIGTRAP == trace trap    */
  176.   4,            /* pretend SIGABRT is also illegal instruction */
  177.   8,            /* SIGPRIV == privileged instruction exception */
  178.   5,            /* SIGFPE == divide by zero */
  179.   0, 2,            /* SIGBUS == bus error */
  180.   3            /* SIGSEGV == address error */
  181. /* everything else gets zeros */
  182. };
  183.  
  184. /* a "0" means we don't print a message when it happens -- typically the
  185.    user is expecting a synchronous signal, so we don't need to report it
  186. */
  187.  
  188. const char *signames[NSIG] = { 0,
  189. 0, 0, 0, "ILLEGAL INSTRUCTION", "TRACE TRAP",
  190. 0, "PRIVILEGE VIOLATION", "DIVISION BY ZERO", 0, "BUS ERROR",
  191. "ADDRESS ERROR", "BAD SYSTEM CALL", 0, 0, 0,
  192. 0, 0, 0, 0, 0,
  193. 0, 0, 0, "CPU TIME EXHAUSTED", "FILE TOO BIG",
  194. 0, 0, 0, 0, 0
  195. };
  196.  
  197. /*
  198.  * replaces the TOS "show bombs" routine: for now, print the name of the
  199.  * interrupt on the console, and save info on the crash in the appropriate
  200.  * system area
  201.  */
  202.  
  203. void
  204. bombs(sig)
  205.     int sig;
  206. {
  207.     long *procinfo = (long *)0x380L;
  208.     int i;
  209.     CONTEXT *crash;
  210.     extern int no_mem_prot;
  211.  
  212.     if (sig < 0 || sig > 31) {
  213.         ALERT("bombs(%d): sig out of range", sig);
  214.     }
  215.     else if (signames[sig]) {
  216.         if (!no_mem_prot && sig == SIGBUS) {
  217.             /* already reported by report_buserr */
  218.         } else {
  219.             ALERT("%s: User PC=%lx (basepage=%lx)",
  220.                 signames[sig],
  221.                 curproc->exception_pc, curproc->base);
  222.         }
  223. /* save the processor state at crash time */
  224. /* assumes that "crash time" is the context curproc->ctxt[SYSCALL] */
  225. /* BUG: this is not true if the crash happened in the kernel; in the
  226.  * latter case, the crash context wasn't saved anywhere.
  227.  */
  228.         crash = &curproc->ctxt[SYSCALL];
  229.         *procinfo++ = 0x12345678L;    /* magic flag for valid info */
  230.         for (i = 0; i < 15; i++)
  231.             *procinfo++ = crash->regs[i];
  232.         *procinfo++ = curproc->exception_ssp;
  233.         *procinfo++ = ((long)excep_num[sig]) << 24L;
  234.         *procinfo = crash->usp;
  235.  
  236. /* we're also supposed to save some info from the supervisor stack. it's not
  237.  * clear what we should do for MiNT, since most of the stuff that used to be
  238.  * on the stack has been put in the CONTXT struct. Moreover, we don't want
  239.  * to crash because of an attempt to access illegal memory. Hence, we do
  240.  * nothing here...
  241.  */
  242.     } else {
  243.         TRACE(("bombs(%d)", sig));
  244.     }
  245. }
  246. #endif
  247.  
  248. /*
  249.  * handle_sig: do whatever is appropriate to handle a signal
  250.  */
  251.  
  252. static long unwound_stack = 0;
  253.  
  254. void
  255. handle_sig(sig)
  256.     int sig;
  257. {
  258.     long oldstack, newstack;
  259.     long *stack;
  260.     CONTEXT *call, contexts[2];
  261. #define oldsysctxt (contexts[0])
  262. #define newcurrent (contexts[1])
  263.  
  264.     extern void sig_return();
  265.  
  266.     if (curproc->sighandle[sig] == SIG_IGN)
  267.         return;
  268.     ++curproc->nsigs;
  269.     if (curproc->sighandle[sig] == SIG_DFL) {
  270. _default:
  271.         switch(sig) {
  272. #if 0
  273. /* Note: SIGNULL is filtered out in dossig.c and is never actually
  274.  * delivered (its only purpose for the user is to test for the existence of
  275.  * a process, it isn't a real signal). The kernel uses SIGNULL
  276.  * internally, but all such code does the signal handling "by hand"
  277.  * and so no default handling is necessary.
  278.  */
  279.         case SIGNULL:
  280. #endif
  281.         case SIGWINCH:
  282.         case SIGCHLD:
  283. /* SIGFPE is divide by 0; TOS ignores this, so we will too */
  284.         case SIGFPE:
  285.             return;        /* do nothing */
  286.         case SIGSTOP:
  287.         case SIGTSTP:
  288.         case SIGTTIN:
  289.         case SIGTTOU:
  290.             stop(sig);
  291.             return;
  292.         case SIGCONT:
  293.             curproc->sigpending &= ~STOPSIGS;
  294.             return;
  295.  
  296. /* here are the fatal signals. for SIGINT, we use p_term(-32) so that
  297.  * TOS programs that catch ^C via the vector at 0x400 and which expect
  298.  * TOS's error code (-32) to be sent will work. For most other signals,
  299.  * we p_term with an error code; for SIGKILL, we don't want to allow
  300.  * the program any chance to recover, so we call terminate() directly
  301.  * to avoid calling through to the user's terminate vector.
  302.  */
  303.         case SIGINT:        /* ^C */
  304.             if (curproc->domain == DOM_TOS) {
  305.                 p_term(-32);
  306.                 return;
  307.             }
  308.             /* otherwise, fall through */
  309.         default:
  310. #ifdef EXCEPTION_SIGS
  311.             bombs(sig); /* tell the user what happened */
  312. #endif
  313.     /* the "sigmask" check is in case a bus error happens in the user's
  314.      * term_vec code; we don't want to get stuck in an infinite loop!
  315.      */
  316.             if ((curproc->sigmask & 1L) || sig == SIGKILL)
  317.                 terminate(sig << 8, ZOMBIE_Q);
  318.             else
  319.                 p_term(sig << 8);
  320.         }
  321.     }
  322.     else {        /* user wants to handle it himself */
  323.  
  324. /* another kludge: there is one case in which the p_sigreturn mechanism
  325.  * is invoked by the kernel, namely when the user calls Supexec()
  326.  * or when s/he installs a handler for the GEMDOS terminate vector (#0x102)
  327.  * and the program terminates. MiNT fakes the call to user code with
  328.  * signal 0 (SIGNULL); programs that longjmp out of the user function
  329.  * and are later sent back to it again (e.g. if ^C keeps getting pressed
  330.  * and a terminate vector has been installed) will grow the stack without
  331.  * bound unless we watch for this case.
  332.  *
  333.  * Solution (sort of): whenever Pterm() is called, we unwind the
  334.  * stack; otherwise, we let it grow, so that nested Supexec()
  335.  * calls work.
  336.  *
  337.  * Note that SIGNULL is thrown away when sent by user processes, 
  338.  * and the user can't mask it (it's UNMASKABLE), so there is
  339.  * is no possibility of confusion with anything the user does.
  340.  */
  341.         if (sig == 0) {
  342.     /* p_term() sets sigmask to let us know to do Psigreturn */
  343.             if (curproc->sigmask & 1L) {
  344.                 p_sigreturn();
  345.                 curproc->sigmask &= ~1L;
  346.             } else {
  347.                 unwound_stack = 0;
  348.             }
  349.         }
  350.  
  351.         call = &curproc->ctxt[SYSCALL];
  352. /*
  353.  * what we do is build two fake stack frames; the bottom one is
  354.  * for a call to the user function, with (long)parameter being the
  355.  * signal number; the top one is for sig_return.
  356.  * When the user function returns, it returns to sig_return, which
  357.  * calls into the kernel to restore the context in prev_ctxt
  358.  * (thus putting us back here). We can then continue on our way.
  359.  */
  360.  
  361. /* set a new system stack, with a bit of buffer space */
  362.         oldstack = curproc->sysstack;
  363.         newstack = ((long) ( (&newcurrent) - 2 )) - 12;
  364.  
  365.         if (newstack < (long)curproc->stack + ISTKSIZE + 256) {
  366.             ALERT("stack overflow");
  367.             goto _default;
  368.         }
  369.         else if ((long) curproc->stack + STKSIZE < newstack) {
  370.             FATAL("system stack not in proc structure");
  371.         }
  372.  
  373. /* unwound_stack is set by p_sigreturn() */
  374.         if (sig == 0 && unwound_stack)
  375.             curproc->sysstack = unwound_stack;
  376.         else
  377.             curproc->sysstack = newstack;
  378.         oldsysctxt = *call;
  379.         stack = (long *)(call->sr & 0x2000 ? call->ssp :
  380.                 call->usp);
  381. /*
  382.    Hmmm... here's another potential problem for the signal 0 terminate
  383.    vector: if the program keeps returning back to user mode without
  384.    worrying about the supervisor stack, we'll eventually overflow it.
  385.    However, if the program is in supervisor mode itself, then we don't
  386.    want to stomp on its stack. Temporary solution: ignore the problem,
  387.    the stack's only growing 12 bytes at a time.
  388.  */
  389. /*
  390.  * in addition to the signal number we stuff the vector offset on the
  391.  * stack; if the user is interested they can sniff it, if not ignoring
  392.  * it needs no action on their part. Why do we need this? So that a
  393.  * single SIGFPE handler (for example) can discriminate amongst the
  394.  * multiple things which may get thrown its way
  395.  */
  396.         *(--stack) = (long)call->sfmt & 0xfff;
  397.         *(--stack) = (long)sig;
  398.         *(--stack) = (long)sig_return;
  399.         if (call->sr & 0x2000)
  400.             call->ssp = ((long) stack);
  401.         else
  402.             call->usp = ((long) stack);
  403.         call->pc = (long) curproc->sighandle[sig];
  404.         call->sfmt = call->fstate[0] = 0;    /* don't restart FPU communication */
  405.  
  406.         ((long *)curproc->sysstack)[1] = FRAME_MAGIC;
  407.         ((long *)curproc->sysstack)[2] = oldstack;
  408.         ((long *)curproc->sysstack)[3] = sig;
  409.  
  410.         if (curproc->sigflags[sig] & SA_RESET) {
  411.             curproc->sighandle[sig] = SIG_DFL;
  412.             curproc->sigflags[sig] &= ~SA_RESET;
  413.         }
  414.             
  415.         if (save_context(&newcurrent) == 0 ) {
  416. /*
  417.  * go do the signal; eventually, we'll restore this context (unless the
  418.  * user longjmp'd out of his signal handler). while the user is handling
  419.  * the signal, it's masked out to prevent race conditions. p_sigreturn()
  420.  * will unmask it for us when the user is finished.
  421.  */
  422.             newcurrent.regs[0] = CTXT_MAGIC;
  423.                 /* set D0 so next return is different */
  424.             assert(curproc->magic == CTXT_MAGIC);
  425.             leave_kernel();
  426.             restore_context(call);
  427.         }
  428. /*
  429.  * OK, we get here from p_sigreturn, via the user returning from
  430.  * the handler to sig_return. Restoring the stack and unmasking the
  431.  * signal have been done already for us by p_sigreturn.
  432.  * We should just restore the old system call context
  433.  * and continue with whatever it was we were doing.
  434.  */
  435.         TRACE(("done handling signal"));
  436.         curproc->ctxt[SYSCALL] = oldsysctxt;
  437.         assert(curproc->magic == CTXT_MAGIC);
  438.     }
  439. #undef oldsysctxt
  440. #undef newcurrent
  441. }
  442.  
  443. /*
  444.  * the p_sigreturn system call
  445.  * When called by the user from inside a signal handler, it indicates a
  446.  * desire to restore the old stack frame prior to a longjmp() out of
  447.  * the handler.
  448.  * When called from the sig_return module, it indicates that the user
  449.  * is finished a handler, and we should not only restore the stack
  450.  * frame but also the old context we were working in (which is on the
  451.  * system call stack -- see handle_sig).
  452.  * The "valid_return" variable is 0 in the first case, 1 in the second.
  453.  */
  454.  
  455. short valid_return;
  456.  
  457. long ARGS_ON_STACK
  458. p_sigreturn()
  459. {
  460.     CONTEXT *oldctxt;
  461.     long *frame;
  462.     long sig;
  463.  
  464.     unwound_stack = 0;
  465. top:
  466.     frame = (long *)curproc->sysstack;
  467.     frame++;    /* frame should point at FRAME_MAGIC, now */
  468.     sig = frame[2];
  469.     if (*frame != FRAME_MAGIC || (sig < 0) || (sig >= NSIG)) {
  470.         FATAL("Psigreturn: system stack corrupted");
  471.     }
  472.     if (frame[1] == 0) {
  473.         DEBUG(("Psigreturn: frame at %lx points to 0", frame-1));
  474.         return 0;
  475.     }
  476.     unwound_stack = curproc->sysstack;
  477.     TRACE(("Psigreturn(%d)", (int)sig));
  478.  
  479.     curproc->sysstack = frame[1];    /* restore frame */
  480.     curproc->sigmask &= ~(1L<<sig); /* unblock signal */
  481.  
  482.     if (!valid_return) {
  483. /* here, the user is telling us that a longjmp out of a signal handler is
  484.  * about to occur; so we should unwind *all* the signal frames
  485.  */
  486.         goto top;
  487.     }
  488.     else {
  489.         valid_return = 0;
  490.         oldctxt = ((CONTEXT *)(&frame[2])) + 2;
  491.         if (oldctxt->regs[0] != CTXT_MAGIC) {
  492.             FATAL("p_sigreturn: corrupted context");
  493.         }
  494.         assert(curproc->magic == CTXT_MAGIC);
  495.         restore_context(oldctxt);
  496.         return 0;    /* dummy -- this isn't reached */
  497.     }
  498. }
  499.  
  500. /*
  501.  * stop a process because of signal "sig"
  502.  */
  503.  
  504. void
  505. stop(sig)
  506.     int sig;
  507. {
  508.     unsigned int code;
  509.     unsigned long oldmask;
  510.     PROC *p;
  511.  
  512.     code = sig << 8;
  513.  
  514.     if (curproc->pid == 0) {
  515.         FORCE("attempt to stop MiNT");
  516.         return;
  517.     }
  518.  
  519. /* notify parent */
  520.     if (curproc->ptracer) {
  521.         p = curproc->ptracer;
  522.         post_sig(p, SIGCHLD);
  523.     } else {
  524.         p = pid2proc(curproc->ppid);
  525.         if (p && !(p->sigflags[SIGCHLD] & SA_NOCLDSTOP))
  526.             post_sig(p, SIGCHLD);
  527.     }
  528.  
  529.     oldmask = curproc->sigmask;
  530.  
  531.     if ((1L << sig) & STOPSIGS) {
  532.         /* mask out most signals */
  533.         curproc->sigmask |= ~(UNMASKABLE | SIGTERM);
  534.     }
  535.     else
  536.         assert(curproc->ptracer);
  537.  
  538. /* sleep until someone signals us awake */
  539.     sleep(STOP_Q, (long) code | 0177);
  540.  
  541. /* when we wake up, restore the signal mask */
  542.     curproc->sigmask = oldmask;
  543.  
  544. /* and discard any signals that would cause us to stop again */
  545.     curproc->sigpending &= ~STOPSIGS;
  546. }
  547.  
  548. /*
  549.  * interrupt handlers to raise SIGBUS, SIGSEGV, etc. Note that for
  550.  * really fatal errors we reset the handler to SIG_DFL, so that
  551.  * a second such error kills us
  552.  */
  553.  
  554. void
  555. exception(sig)
  556.     int sig;
  557. {
  558.     curproc->sigflags[sig] |= SA_RESET;
  559.     DEBUG(("exception #%d raised", sig));
  560.     raise(sig);
  561. }
  562.  
  563. void
  564. sigbus()
  565. {
  566.     if (curproc->sighandle[SIGBUS] == SIG_DFL)
  567.         report_buserr();
  568.     exception(SIGBUS);
  569. }
  570.  
  571. void
  572. sigaddr()
  573. {
  574.     exception(SIGSEGV);
  575. }
  576.  
  577. void
  578. sigill()
  579. {
  580.     exception(SIGILL);
  581. }
  582.  
  583. void
  584. sigpriv()
  585. {
  586.     raise(SIGPRIV);
  587. }
  588.  
  589. void
  590. sigfpe()
  591. {
  592.     extern short fpu;    /* in main.c */
  593.     
  594.     if (fpu) {
  595.         CONTEXT *ctxt;
  596.  
  597.         ctxt = &curproc->ctxt[SYSCALL];
  598.  
  599.     /* 0x1f38 is a Motorola magic cookie to detect a 68882 idle state frame */
  600.         if (*(ushort *)ctxt->fstate == 0x1f38 && 
  601.             (ctxt->sfmt & 0xfff) >= 0xc0L && (ctxt->sfmt & 0xfff) <= 0xd8L) {
  602.             /* fix a bug in the 68882 - Motorola call it a feature :-) */
  603.             ctxt->fstate[ctxt->fstate[1]] |= 1 << 3;
  604.         }
  605.     }
  606.     raise(SIGFPE);
  607. }
  608.  
  609. void
  610. sigtrap()
  611. {
  612.     raise(SIGTRAP);
  613. }
  614.  
  615. void
  616. haltformat()
  617. {
  618.     FATAL("halt: invalid stack frame format");
  619. }
  620.  
  621. void
  622. haltcpv()
  623. {
  624.     FATAL("halt: coprocessor protocol violation");
  625. }
  626.